package com.kizuki.io;

import org.junit.Test;
import java.io.*;

public class TestConvertAndPrint {

    /**
     * InputStreamReader 字节 -> 字符
     * OutputStreamWriter 字符 -> 字节
     */
    @Test
    public void testConvert() throws IOException {
        Reader inputStreamReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream("新垣结衣".getBytes()), "utf-8"));
        int len;
        while((len = inputStreamReader.read()) != -1) {
            System.out.print((char)len);
        }
        System.out.println();
        inputStreamReader.close();

        String fileName = "F:\\outPut.txt";
        File file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        Writer outputStreamWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
        outputStreamWriter.write("石原里美");
        outputStreamWriter.flush();
        outputStreamWriter.close();
    }

    /**
     * PrintStream System.out最终输出的总是byte数据
     * PrintWriter 最终输出的是char数据
     */
    @Test
    public void testPrint() throws IOException {
        String fileName = "F:\\outPut.txt";
        File file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        PrintWriter write = new PrintWriter(new FileOutputStream(fileName));
        /**
         * println 是write的包装，可以写入各种类型
         * write 仅支持输出字符类型数据
         */
        write.println(new char[]{'f', 'l', 'y'});
        write.println(100_22_3L);
        write.println("english");
        write.write("这是一句中文\n");
        write.write("测试换行");
        write.flush();
        write.close();
    }


}
